const path = require('path');

const packConfig = {
    'pkg/npm.js': [
        '/static/mod.js',
        '/node_modules/**.js',
        '!/node_modules/monaco-editor/**'
    ],

    'pkg/aio.js': [
        '/page/index.jsx',
        '/page/index.jsx:deps'
    ],

    'pkg/rest.js': [
        '/**.{js,jsx}',
        '!/static/mod.js',
        '!/node_modules/monaco-editor/**',
        '!/widgets/editor/worker-loader-proxy.js',
        '!/renderers/form/region/picker.js'
    ],

    'pkg/style.css': [
        'bootstrap.css',
        'font-awesome.css',
        'node_modules/*/**.css',
        '/static/less/app.less',
        '/**.{scss,less,css}',
        '!/static/scss/style.scss',
        '/static/scss/style.scss'
    ]
};

fis.set('project.files', [
    '/page/*.html',
    'mock/**'
]);

fis.set('namespace', 'admin');

fis.hook('node_modules', {
    shimBuffer: false,
    shimProcess: false
});
fis.hook('commonjs', {
    packages: [
        {
            name: 'formsy-react',
            location: './renderers/form/formsy',
            main: 'main.jsx'
        }
    ]
});

fis.match('*.min.js', {
    optimizer: null
});

fis.match('/node_modules/**.js', {
    isMod: true
});

fis.match('{/{actions, reducers, renderers, routes, widgets, util, editor}/**.js, *.jsx}', {
    rExt: 'js',
    parser: fis.plugin('typescript', {
        sourceMap: true,
        noImplicitUseStrict: true
    }),
    preprocessor: [fis.plugin('js-require-css'), fis.plugin('js-require-file')],
    isMod: true
});

fis.match('/widgets/editor/worker-loader-proxy.js', {
    isMod: false
});

fis.match('/node_modules/{entities,htmlparser2,css-select, cheerio}/**.js', {
    preprocessor: fis.plugin('js-require-file')
});

// 用 node-sass 解析
fis.match('*.scss', {
    rExt: 'css',
    parser: fis.plugin('node-sass', {
        include_paths: [
            path.join(__dirname, 'static/scss')
        ]
    })
});

// 用 node-sass 解析
fis.match('*.less', {
  rExt: 'css',
  parser: fis.plugin('less')
});

// 纯前端的项目，用 loader 来自动引入资源。
fis.match('::package', {
    postpackager: [
        fis.plugin('loader', {
            useInlineMap: false,
            resourceType: 'mod'
        })
    ]
});

fis.match('/node_modules/**.js', {
    packTo: 'aio.js'
});

fis.match('/node_modules/brace/worker/**.js', {
    skipBrowserify: true
});

fis.match('/node_modules/monaco-editor/**', {
    standard: false,
    isVscFile: true,
    isMod: false,
    packTo: null,
    optimizer: false
});

fis.match('*', {
  release: '/public/${namespace}/$0',
  url: '/${namespace}$0'
});

fis.match('/page/(**).html', {
  release: '/resources/views/${namespace}/$1.blade.php',
  rExt: '.blade.php',
  isHtmlLike: true
});

fis.match('**/(*.{eot,svg,ttf,woff,woff2})', {
  release: '/public/${namespace}/fonts/$1',
  url: '/${namespace}/fonts/$1'
});

var dev = fis.media('devLocal');

dev.match('*', {
  release: '$0',
  url: '$0'
});

dev.match('/node_modules/**.js', {
    packTo: 'npm.js'
});

dev.match('/node_modules/**.css', {
    packTo: 'npm.css'
});


dev.match('/page/(**.html)', {
    release: '/admin/$1',
    rExt: '.html'
});

dev.match('*', {
    optimizer: null,
    useHash: false,
    deploy: [
        fis.plugin('skip-packed'),

        fis.plugin('local-deliver', {
            to: './output'
        })
    ]
});

var prod = fis.media('prod');
prod.match('::package', {
    packager: fis.plugin('deps-pack', packConfig)
});

prod.match('**.{js,css,less,scss,es6,jsx,jpg,jpeg,png,gif,eot,svg,ttf,woff,woff2}', {
    useHash: true
});

prod.match('*.map', {
    release: false,
    url: 'null',
    useHash: false
});

prod.match('/**.{js,jsx}', {
    moduleId: function (m, path) {
        return fis.util.md5('amis.baidu.com' + path);
    }
});

prod.match('{/{actions, reducers, renderers, routes, widgets, util}/**.js, *.jsx}', {
    parser: fis.plugin('typescript', {
        sourceMap: false,
        noImplicitUseStrict: true
    })
});

prod.match('*.{js,jsx}', {
    optimizer: fis.plugin('uglify-js')
});

prod.match('*.{css,less,scss}', {
  optimizer: fis.plugin('clean-css')
});

prod.match('*.min.js', {
  optimizer: null
});

prod.match('*', {
    deploy: [
        fis.plugin('skip-packed'),


        fis.plugin('local-deliver', {
            to: '../Volunteer_Admin_PHP/'
        })
    ]
});
